(function( factory ) {
	define( ["jquery", "jquery.validate"], factory );
}(function( $ ) {
	$.extend( $.validator.messages, {
		required: "这是必填字段",
		remote: "请修正此字段",
		email: "请输入有效的电子邮件地址",
		url: "请输入有效的网址",
		date: "请输入有效的日期",
		dateISO: "请输入有效的日期 (YYYY-MM-DD)",
		number: "请输入有效的数字",
		digits: "只能输入数字",
		creditcard: "请输入有效的信用卡号码",
		equalTo: "你的输入不相同",
		extension: "请输入有效的后缀",
		maxlength: $.validator.format( "最多可以输入 {0} 个字符" ),
		minlength: $.validator.format( "最少要输入 {0} 个字符" ),
		rangelength: $.validator.format( "请输入长度在 {0} 到 {1} 之间的字符串" ),
		range: $.validator.format( "请输入范围在 {0} 到 {1} 之间的数值" ),
		max: $.validator.format( "请输入不大于 {0} 的数值" ),
		min: $.validator.format( "请输入不小于 {0} 的数值" ),
        isBlank: $.validator.format( "不能全输入空格" ),
        noChinese:$.validator.format( "不能输入中文" ),
	});


    jQuery.validator.addMethod("noChinese", function(value, element) {
        var blank = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
        return this.optional(element)||!(blank.test(value));
    }, "不能输入中文");

    // 全空格验证
    $.validator.addMethod("isBlank", function(value, element) {
        var blank = /^[ ]*$/;
        return this.optional(element) || !(blank.test(value));
    }, "不能全输入空格");

    // 验证字符串是否为xml格式
    $.validator.addMethod("isXml", function(value, element) {
        //errorCode 0是xml正确，1是xml错误，2是无法验证
        var xmlDoc,errorMessage,errorCode = 0;
        var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
        // var isIE = userAgent.indexOf("compatible") > -1
        //     && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器

        // code for IE
        if ((userAgent.indexOf("Trident"))!=-1)
        {
            xmlDoc  = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async="false";
            xmlDoc.loadXML(value);

            if(xmlDoc.parseError.errorCode!=0)
            {
                errorMessage="错误code: " + xmlDoc.parseError.errorCode + "\n";
                errorMessage=errorMessage+"错误原因: " + xmlDoc.parseError.reason;
                errorMessage=errorMessage+"错误位置: " + xmlDoc.parseError.line;
                errorCode = 1;
            }
            else
            {
                errorMessage = "格式正确";
            }
        }
        // code for Mozilla, Firefox, Opera, chrome, safari,etc.
        else if (document.implementation.createDocument)
        {
            var parser=new DOMParser();
            xmlDoc = parser.parseFromString(value,"text/xml");
            var error = xmlDoc.getElementsByTagName("parsererror");
            if (error.length > 0)
            {
                if(xmlDoc.documentElement.nodeName=="parsererror"){
                    errorCode = 1;
                    errorMessage = xmlDoc.documentElement.childNodes[0].nodeValue;
                } else {
                    errorCode = 1;
                    errorMessage = xmlDoc.getElementsByTagName("parsererror")[0].innerHTML;
                }
            }
            else
            {
                errorMessage = "格式正确";
            }
        }
        else
        {
            errorCode = 2;
            errorMessage = "浏览器不支持验证，无法验证xml正确性";
        }
        return this.optional(element) || (errorCode == 0);
    }, "这不是个xml");


    // 验证字符串是否为JSON格式
    $.validator.addMethod("isJSON", function(value, element) {
        var flag=true;
        try {
            var obj=JSON.parse(value);
            flag = true;
        } catch(e) {
            flag = false;
        }
        return this.optional(element) || (flag);
    }, "这不是个json");

    // 验证字符串是否为YAML格式
    $.validator.addMethod("isYAML", function(value, element) {
        var flag=true;
        try {
            var obj=JSON.parse(value);
            flag = true;
        } catch(e) {
            flag = false;
        }
        return this.optional(element) || (flag);
    }, "这不是个json");

    // 不能为汉字
	$.validator.addMethod("notChinese", function(value, element) {
		var flag=true;
		var reg = /[\u4E00-\u9FA5]/;
		if(!reg.test(value)){
			flag=true;
		}else{
			flag=false;
		}
		return this.optional(element)||(flag);
	}, "不能含有汉字");
	
	$.extend( $.validator.defaults , {
		ignore: ":hidden:not(select)",
		errorPlacement: function ( error, element ) {
			//error.addClass("error-block");
			//error.insertAfter(element.parent(".validate"));
			error.addClass("bottom-error-block");
			element.parent(".validate").append(error);
		},
		highlight : function ( element, errorClass, validClass ) {
			$(element).parents(".validate").addClass("has-error").removeClass("has-success");
		},
		unhighlight : function ( element, errorClass, validClass ) {
			$( element ).parents(".validate").addClass("has-success").removeClass("has-error");
		}
	});
}));
